package com.bwhx.eruler.controller;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.bwhx.eruler.bean.SdkData;
import com.bwhx.eruler.service.SdkDataService;
import com.fasterxml.jackson.databind.ObjectMapper;

/**
 * 苹果sdk上传的数据
 * @author Cuilitang
 *
 */
@Controller
public class SdkDataController extends BaseController {
	
	@Autowired
	SdkDataService sdkdataService;
	
	@ResponseBody
	@RequestMapping(value = "/sdk.UpLoadSCandata.ashx",method={RequestMethod.POST})
	public Map<String,String> upload(HttpServletRequest request){
		String params = (String)request.getAttribute("params");
		Map<String,String> map = new HashMap<String,String>();
		map.put("meg", "上传成功");
		map.put("status", "1");
		ObjectMapper objectMapper = new ObjectMapper();
	
		try{
			if(request.getHeader("Content-type").contains("json")){//苹果
		
				SdkData sdkdata = objectMapper.readValue(params, SdkData.class);
				sdkdata.setScanData(objectMapper.writeValueAsString(sdkdata.getScanData()));
				sdkdata.setUserData(objectMapper.writeValueAsString(sdkdata.getUserData()));
				sdkdata.setDateTime(System.currentTimeMillis()/1000);
				sdkdataService.insert(sdkdata);
			}else{//安卓
				
				map.put("meg", "不支持的格式！");
				map.put("status", "0");
				return map;
				
				
			}
	
		}catch(Exception e){
			log.error(getTrace(e));
			map.put("meg", "上传失败！");
			map.put("status", "0");
		}
				
	    return map;
	}	
	
	
	
}
